﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;
using System.Configuration;

namespace Prodma.DataAccess.DataService
{
    public class GalleryCategoryService : IGalleryCategoryRepository
    {

        private DataContext context = new DataContext();

        #region IRecordOperation<tb_GalleryCategory,GalleryCategoryVm> Members

        public IList<GalleryCategoryVm> GetList()
        {
            throw new NotImplementedException();
        }

        public void New(tb_GalleryCategory entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_GalleryCategory(entity);
                context.SaveChanges();
            }
        }
        public GalleryCategoryVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from category in context.tb_GalleryCategory
                            where category.Id == id
                            select new GalleryCategoryVm()
                            {
                                Id = category.Id,
                                Name = category.Name,
                                Description = category.Description,
                                Image = category.Image,
                                Folder = category.Folder,
                                Area = new AreaVm() { Id = category.tb_Area.Id, Name = category.tb_Area.Name },
                                State = new StateVm() { Id = category.tb_State.Id, Name = category.tb_State.Name }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_GalleryCategory entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from category in context.tb_GalleryCategory
                            where category.Id == id
                            select category).FirstOrDefault();

                edit.Name = entity.Name;
                edit.Description = entity.Description;
                edit.Image = entity.Image;
                edit.Folder = entity.Folder;
                edit.tb_AreaReference.EntityKey = entity.tb_AreaReference.EntityKey;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from category in context.tb_GalleryCategory
                              where category.Id == id
                              select category).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IPageOperation<GalleryCategoryVm> Members

        public IList<GalleryCategoryVm> GetList(int page)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from category in context.tb_GalleryCategory
                            select new GalleryCategoryVm()
                            {
                                Id = category.Id,
                                Name = category.Name,
                                Area = new AreaVm() { Id = category.tb_Area.Id, Name = category.tb_Area.Name },
                                State = new StateVm() { Id = category.tb_State.Id, Name = category.tb_State.Name }
                            }).OrderBy(x => x.Name).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from category in context.tb_GalleryCategory
                                select category).Count();

                return rowCount;
            }
        }
        #endregion

        #region IPageableFindOperation<GalleryCategoryVm> Members

        public IList<GalleryCategoryVm> GetList(int page, Dictionary<string, string> find)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;

            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from category in context.tb_GalleryCategory
                            where category.Name.StartsWith(findName)
                            select new GalleryCategoryVm()
                            {
                                Id = category.Id,
                                Name = category.Name,
                                Area = new AreaVm() { Id = category.tb_Area.Id, Name = category.tb_Area.Name },
                                State = new StateVm() { Id = category.tb_State.Id, Name = category.tb_State.Name }
                            }).OrderBy(x => x.Name).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from category in context.tb_GalleryCategory
                                where category.Name.StartsWith(findName)
                                select category).Count();

                return rowCount;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from category in context.tb_GalleryCategory
                                 where category.Id == id
                                 select category).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from category in context.tb_GalleryCategory
                                 where category.Id == id
                                 select category).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

        #region IImageOperation Members

        public void RemoveImage(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var removeImage = (from category in context.tb_GalleryCategory
                                   where category.Id == id
                                   select category).FirstOrDefault();

                removeImage.Image = ConfigurationManager.AppSettings["galleryBlankImage"].ToString();

                context.SaveChanges();
            }
        }
        #endregion
        

    }
}
